package com.example.hospital.service;

import com.example.hospital.common.PageResponse;
import com.example.hospital.domain.entity.Doctor;
import com.example.hospital.domain.entity.DoctorSchedule;
import com.example.hospital.domain.entity.ExaminationOrder;
import com.example.hospital.domain.entity.IndicatorResult;
import com.example.hospital.domain.entity.Sample;
import com.example.hospital.mapper.DoctorMapper;
import com.example.hospital.mapper.DoctorScheduleMapper;
import com.example.hospital.mapper.ExaminationOrderMapper;
import com.example.hospital.mapper.IndicatorResultMapper;
import com.example.hospital.mapper.SampleMapper;
import org.springframework.stereotype.Service;

import java.time.LocalDateTime;
import java.util.List;

@Service
public class DoctorService {
    private final DoctorMapper doctorMapper;
    private final DoctorScheduleMapper scheduleMapper;
    private final ExaminationOrderMapper orderMapper;
    private final SampleMapper sampleMapper;
    private final IndicatorResultMapper indicatorResultMapper;
    /**
     * 构造函数
     * @param doctorMapper
     * @param scheduleMapper
     * @param orderMapper
     * @param sampleMapper
     * @param indicatorResultMapper
     */
    public DoctorService(DoctorMapper doctorMapper, DoctorScheduleMapper scheduleMapper, ExaminationOrderMapper orderMapper, SampleMapper sampleMapper, IndicatorResultMapper indicatorResultMapper) {
        this.doctorMapper = doctorMapper;
        this.scheduleMapper = scheduleMapper;
        this.orderMapper = orderMapper;
        this.sampleMapper = sampleMapper;
        this.indicatorResultMapper = indicatorResultMapper;
    }
    /**
     * 医生登录
     * @param phone
     * @param password
     * @return
     */
    public Doctor loginByPhone(String phone, String password) {
        Doctor doctor = doctorMapper.findByPhone(phone);
        if (doctor == null) throw new IllegalArgumentException("医生不存在或手机号错误");
        // 明文校验：若密码未设置，提示管理员设置
        String stored = doctor.getPassword();
        if (stored == null || stored.isEmpty()) {
            throw new IllegalArgumentException("医生账户未设置密码，请联系管理员");
        }
        if (!password.equals(stored)) {
            throw new IllegalArgumentException("密码错误");
        }
        return doctor;
    }
    /**
     * 更新医生个人信息
     * @param doctor
     * @return
     */
    /**
     * 更新医生个人信息
     * @param doctor
     * @return
     */
    /**
     * 更新医生个人信息
     * @param doctor
     * @return
     */
    public boolean updateProfile(Doctor doctor) {
        return doctorMapper.update(doctor) > 0;
    }
    /**
     * 获取医生排班列表
     * @param doctorId
     * @param date
     * @param page
     * @param size
     * @return
     */
    public PageResponse<DoctorSchedule> listSchedules(Long doctorId, java.time.LocalDate date, int page, int size) {
        int offset = (page - 1) * size;
        List<DoctorSchedule> list = scheduleMapper.list(doctorId, date, size, offset);
        long total = scheduleMapper.count(doctorId, date);
        return PageResponse.of(total, page, size, list);
    }
    /**
     * 获取医生已分配订单列表
     * @param doctorId
     * @param status
     * @param orderType
     * @param page
     * @param size
     * @return
     */
    public PageResponse<ExaminationOrder> listAssignedOrders(Long doctorId, Integer status, String orderType, int page, int size) {
        int offset = (page - 1) * size;
        List<ExaminationOrder> list = orderMapper.list(null, doctorId, status, orderType, size, offset);
        long total = orderMapper.count(null, doctorId, status, orderType);
        return PageResponse.of(total, page, size, list);
    }
    /**
     * 创建样本
     * @param orderId
     * @param sampleType
     * @param remark
     * @return
     */
    public Sample createSample(Long orderId, String sampleType, String remark) {
        Sample s = new Sample();
        s.setOrderId(orderId);
        s.setSampleType(sampleType);
        s.setCollectionTime(LocalDateTime.now());
        s.setStatus(2); // 采集成功
        s.setRemark(remark);
        sampleMapper.insert(s);
        return sampleMapper.findById(s.getSampleId());
    }
    /**
     * 更新样本状态
     * @param sampleId
     * @param status
     * @param remark
     * @return
     */
    public boolean updateSampleStatus(Long sampleId, Integer status, String remark) {
        return sampleMapper.updateStatus(sampleId, status, remark) > 0;
    }
    /**
     * 记录指标结果
     * @param result
     * @return
     */
    public IndicatorResult recordIndicatorResult(IndicatorResult result) {
        if (result.getAbnormalFlag() == null) result.setAbnormalFlag(Boolean.FALSE);
        indicatorResultMapper.insert(result);
        return indicatorResultMapper.findById(result.getResultId());
    }
    /**
     * 更新指标结果异常标志
     * @param resultId
     * @param abnormal
     * @param comment
     * @return
     */
    public boolean updateIndicatorResultFlag(Long resultId, Boolean abnormal, String comment) {
        return indicatorResultMapper.updateFlagAndComment(resultId, abnormal, comment) > 0;
    }
}